package com.qfedu.edu.gatewayservice.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author xiaobobo
 * @title: AddTagFilter
 * @projectName seckill-fy-2401-parent
 * @description: TODO
 * @date 2024/11/5  15:27
 */
@Component
public class AddTagFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //这个过滤器的主要功能就是添加tag
        //拿到这个请求
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpRequest newRequest = request.mutate().header("gatewayTag", "gatewayTag").build();
        //然后将这个新的请求放到原始请求中去
        ServerWebExchange exchangeNew = exchange.mutate().request(newRequest).response(exchange.getResponse()).build();
        return chain.filter(exchangeNew);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
